package com.triangle.cashier.widget;


import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.triangle.cashier.R;
import com.triangle.cashier.bean.PayResult;

/**
 * 选择会员
 * Created by  on 2018/6/30.
 */

public class PayResultDialog extends DialogFragment implements View.OnClickListener {

    private OnPayResultClickListener onPayResultClickListener;
    private ImageButton closeBtn;
    private ImageView tipImageView;
    private TextView totalTextView;
    private TextView payResultTextView;
    private TextView orderNumTextView;
    private TextView timeTextView;
    private TextView payWayTextView;
    private ImageButton ticketBtn;
    private ImageButton repayBtn;
    private PayResult payResult;


    private EditText memberNumEditText;


    public PayResultDialog(PayResult payResult, OnPayResultClickListener onPayResultClickListener) {
        this.onPayResultClickListener = onPayResultClickListener;
        this.payResult = payResult;
    }

    public PayResultDialog() {
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View customView = LayoutInflater.from(getActivity()).inflate(
                R.layout.pay_result_dialog, null);
        initView(customView);
        Dialog dialog = new Dialog(getActivity(), R.style.soft_para_dialog);
        dialog.setContentView(customView);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
//        window.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_windows_bg));
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = 500;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        return dialog;
    }

    private void initView(View view) {
        closeBtn = view.findViewById(R.id.close_btn);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onPayResultClickListener.cancel();
            }
        });
        repayBtn = view.findViewById(R.id.repay_btn);
        ticketBtn = view.findViewById(R.id.ticket_btn);
        payWayTextView = view.findViewById(R.id.pay_way_textView);
        timeTextView = view.findViewById(R.id.time_textView);
        orderNumTextView = view.findViewById(R.id.order_num_textView);
        orderNumTextView = view.findViewById(R.id.order_num_textView);
        totalTextView = view.findViewById(R.id.total_TextView);
        payResultTextView = view.findViewById(R.id.pay_result_textView);
        tipImageView = view.findViewById(R.id.pay_tip_imageView);
        orderNumTextView.setText(String.format(getResources().getString(R.string.order_num), payResult.getOrderNumber()));
        timeTextView.setText(String.format(getResources().getString(R.string.order_time), payResult.getTime()));
        if (payResult.getPayWay() == 0) {
            payWayTextView.setText(String.format(getResources().getString(R.string.pay_way), String.format(getResources().getString(R.string.pay_way), getResources().getString(R.string.pay_way_alipay))));
        }
        timeTextView.setText(String.format(getResources().getString(R.string.order_time), payResult.getTime()));
        if (payResult.isSuccess()) {
            tipImageView.setImageResource(R.drawable.pay_success);
            repayBtn.setVisibility(View.GONE);
            ticketBtn.setVisibility(View.VISIBLE);
            payResultTextView.setText(R.string.pay_success);
            totalTextView.setText(payResult.getTotal() + "" + getResources().getString(R.string.pay_unit));

        } else {
            tipImageView.setImageResource(R.drawable.pay_fail);
            repayBtn.setVisibility(View.VISIBLE);
            ticketBtn.setVisibility(View.GONE);
            payResultTextView.setText(R.string.pay_fail);
            totalTextView.setText(R.string.pay_fail_no_money);
        }


    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onStart() {

        super.onStart();
        Window window = this.getDialog().getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        window.getDecorView().setSystemUiVisibility(uiOptions);

    }

    @Override
    public void onClick(View v) {

    }

    public interface OnPayResultClickListener {

        void cancel();

        void ok();

    }

}
